﻿using System;
using System.Data;
using System.Data.Odbc;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace WcfService
{
    [CallbackBehavior(UseSynchronizationContext = false)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
    public class Service1 : IService1
    {

        private OdbcConnection connection;
        public OdbcConnection Connection
        {
            get { return connection; }
            set { connection = value; }
        }
        private string connectionString = string.Empty;
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }

        public string GetData()
        {
            try
            {
                string result = "Pustooo";
                //this.Connection = new OdbcConnection(this.ConnectionString);
                //this.Connection.Open();
                //string query = "SELECT cd.DOCUMENT_ID, cd.WZ_PROMAGG FROM COLLECTOR_DOCUMENTS";
                //OdbcCommand com = new OdbcCommand(query, this.Connection);
                //OdbcDataReader dr = com.ExecuteReader();
                //ListaDanych lista = new ListaDanych();
                //while (dr.Read())
                //{
                //    lista.Lista.Add(dr[0].ToString());
                //}
                //if (lista.Lista.Count == 0)
                //{
                //    lista.Lista.Add("Nic nie ma");
                //}
                result = string.IsNullOrEmpty(this.ConnectionString) ? "Pusto" : this.ConnectionString;
                return result;
            }
            catch (FaultException fe)
            {
                throw new FaultException(fe.Message.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            finally
            {
                //this.Connection.Close();

            }
        }
        /// <summary>
        /// Loguje uzytkownika - tworzy connection dla zapytan do bazy dla okreslonego uzytkownika.
        /// </summary>
        /// <param name="username">Nazwa uzytkownika.</param>
        /// <param name="password">Haslo uzytkownika.</param>
        /// <param name="konfiguratorFile">Dane o bazie danych pobrane z pliku xml uzytkownika.</param>
        /// <returns>Wartosc bool w zaleznosci od udania sie zalogowac lub nie</returns>
        public bool Loguj(string username, string password, string[] konfiguratorFile)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                throw new Exception("Nie wpisano loginu lub hasła .\n");
            }
            string connectionStrWithoutParam = @"DRIVER=Firebird/InterBase(r) driver;UID={0};PWD={1};DBNAME={2}:{3};Charset={4};ROLE={5}";
            try
            {
                this.ConnectionString = String.Format(connectionStrWithoutParam, username.ToUpper(), password, konfiguratorFile[0], konfiguratorFile[1], konfiguratorFile[2], konfiguratorFile[3]);
                this.Connection = new OdbcConnection(this.ConnectionString);
                this.Connection.Open();
                return (this.Connection.State == ConnectionState.Open) ? true : false;
            }
            catch (FaultException ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            catch (Exception e)
            {
                throw new Exception("Nie pamięta się hasełka, nieładnie.\n ", e.InnerException);
            }
            finally
            {
               this.Connection.Close();
            }
        }

        /// <summary>
        /// Zwraca liste dokumentów w magazynie.
        /// </summary>
        /// <returns>Dokumenty</returns>
        public TablicaDanych GetListDocuments()
        {
            try
            {
                this.Connection = new OdbcConnection(this.ConnectionString);
                this.Connection.Open();
                TablicaDanych result = new TablicaDanych();
                string query = "SELECT FIRST 5 cd.DOCUMENT_ID, cd.WZ_PROMAGG FROM COLLECTOR_DOCUMENTS cd order by cd.WZ_PROMAGG ";
                OdbcCommand com = new OdbcCommand(query, this.Connection);
                OdbcDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    result.Tablica.Add(new ComboBoxItem(dr[0].ToString(), dr[1].ToString()));
                }
                //query = "SELECT cd.DOCUMENT_ID, cd.WZ_P OMAGG FROM COLLECTOR_DOCUMENTS cd JOIN KASJER_DOCUMENTS kd on cd.DOCUMENT_ID=kd.DOCUMENT_ID WHERE (kd.USE = 't' or kd.CLOSED = 't')";
                //com = new OdbcCommand(query, this.connection);
                //dr = com.ExecuteReader();
                //while (dr.Read())
                //{
                //    result.Tablica.Remove(new ComboBoxItem(dr[0].ToString(), dr[1].ToString()));
                //}
                return result;
            }
            catch (FaultException ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            finally
            {
                this.Connection.Close();
            }
        }

        public TablicaDanych GetMagazynier()
        {
            try
            {
                this.Connection.Open();
                TablicaDanych result = new TablicaDanych();
                string query = "SELECT m.MAGAZYNIERID, m.MAGAZYNIERID FROM MAGAZYNIERZY m WHERE m.ACTIVITY='T'";
                OdbcCommand com = new OdbcCommand(query, this.Connection);
                OdbcDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    result.Tablica.Add(new ComboBoxItem(dr[0].ToString(), dr[1].ToString()));
                }
                return result;
            }
            catch (FaultException ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            finally
            {
                this.Connection.Close();
            }
        }
    }
}
